package com.gxa.web_admin.controller.propertycontroller;


import com.gxa.common.R;
import com.gxa.common.utils.DateUtils;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
@Api(value = "上传图片", tags = "上传图片接口API_hl")
@RestController
@RequestMapping("/fileupload")
@SuppressWarnings("all")
public class FileUploadController {
	
	@Value("${fileupload.filepath}")
	String filePath;
	
	@Value("${fileupload.server}")
	String serverUrl;
	
	/**
	 * 上传文件
	 */
//	@RequestMapping(value = "/upload",method = RequestMethod.POST)
//	@ApiOperation(value = "图片上传接口-hl",notes = "上传图片",httpMethod = "POST", response = Map.class)
//	public R upload(@RequestParam("file") MultipartFile file) throws Exception {
//
//		R r = new R();
//
//		if (file.isEmpty()){
//			r.setCode(301);
//			r.setMsg("上传文件不能为空");
//			return r;
//		}
//
//		String filename = file.getOriginalFilename();
//
//		String suffix = filename.substring(filename.lastIndexOf("."), filename.length());
//
//		String uuid = UUID.randomUUID().toString();
//
//		String currDate = DateUtils.format(new Date(), "yyyyMMdd");
//
//		FileUtils.writeByteArrayToFile(new File(filePath + "/fileupload/" + currDate + "/" + uuid + suffix), file.getBytes());
//
//		r.setCode(200);
//		r.setMsg("上传图片成功");
//		r.setData(serverUrl + "/" + currDate + "/" + uuid + suffix);
//		return r;
//	}
	@RequestMapping(value = "/upload",method = RequestMethod.POST)
	@ApiOperation(value = "图片上传接口-hl",notes = "上传图片",httpMethod = "POST", response = R.class)
	public R uploadHeadPicture(@RequestParam("file") MultipartFile file) throws Exception {

		R r = new R();
		String filename = file.getOriginalFilename();

		String suffix = filename.substring(filename.lastIndexOf("."), filename.length());

		String uuid = UUID.randomUUID().toString();

		//构造一个带指定 Region 对象的配置类
		Configuration cfg = new Configuration(Region.region0());
		//...其他参数参考类注释

		UploadManager uploadManager = new UploadManager(cfg);
		//...生成上传凭证，然后准备上传
		String accessKey = "5ITUibiIfA-mJ4wWULYqMYPJCI_vbDnH39M7-29D";
		String secretKey = "tH5i_u3MBq3JUjRHaYFpkctvn4oEDl_rMEACAVEj";
		//空间名字
		String bucket = "smartpark1";

		//默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = uuid+suffix;
		String result = null;

		try {
			Auth auth = Auth.create(accessKey, secretKey);
			String upToken = auth.uploadToken(bucket);
			try {
				Response response = uploadManager.put(file.getInputStream(), key, upToken, null, null);
				if (response.statusCode == 200){
					// result = "http://r69tnx16p.hd-bkt.clouddn.com/" + fileName;
					result = "http://rbpd30bnw.hd-bkt.clouddn.com/"+ key;
				}
			} catch (QiniuException ex) {
				Response r1 = ex.response;
				System.err.println(r1.toString());
				try {
					System.err.println(r1.bodyString());
				} catch (QiniuException ex2) {
					//ignore
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		r.setCode(200);
		r.setMsg("上传图片成功");
		r.setData(result);
		return r;
	}
}
